package com.blog.zip.mapper;

import com.blog.zip.bean.Article;
import com.blog.zip.bean.SoloArticle;
import com.blog.zip.bean.Tags;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;
import java.util.Map;

public interface ArticleMapper {

    //查
    @Select("select * from b3_solo_article order by articleCreated desc limit #{offset} , #{pageSize}  ")
    public List<Map<String, Object>> getArticle(Integer offset, Integer pageSize);

    @Select("select oId,articleTitle,articleCreated,articlePermalink,articleStatus from b3_solo_article where " +
        "articleStatus = 0  and articleContent like CONCAT('%',#{keyword},'%')  or articleStatus = 0 and articleTitle like CONCAT('%',#{keyword},'%') ")
    public List<Map<String, Object>> searchArticle(String keyword);

    public Map<String,Object> getArticleById(String id);

    @Select("select c.categoryTitle,c.categoryURI from b3_solo_category as c left join b3_solo_category_tag as ct on ct.category_oId = c.oId left join b3_solo_tag as t on " +
        " t.oId  = ct.tag_oId where t.tagTitle = #{name}")
    public Map<String,Object> getCategoryByTagName(String name);

    // web端
    //@Select("select * from b3_solo_article where oid = #{id} and articleStatus = 0")
    @Select("select * from b3_solo_article where articlePermalink = #{path} and articleStatus = 2")
    public Map<String, Object> getArticleByPath(String path);

//    @Select("SELECT  t.* ,GROUP_CONCAT(b3_solo_category.categoryURI) AS categoryURI,GROUP_CONCAT(b3_solo_category.categoryTitle) AS categoryTitle from b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId = \n" +
//        "                        t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
//        "                       b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId " +
//        "WHERE t.oid = #{id} AND articleStatus = 0 ")
    public Map<String, Object> getArticleByIdV3(String id);

    @Select("SELECT articleViewCount FROM b3_solo_article WHERE oid = #{id} AND articleStatus = 0")
    public Integer getArticleViewCount(String id);

//    @Select("SELECT  t.* ,GROUP_CONCAT(b3_solo_category.categoryURI) AS categoryURI,GROUP_CONCAT(b3_solo_category.categoryTitle) AS categoryTitle from b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId = \n" +
//        "                        t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
//        "                       b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId " +
//        "WHERE t.oid = #{id}")
    public Map<String, Object> getArticleByIdAdmin(String id);

    // web端
    //@Select("select * from b3_solo_article where articlePutTop = #{top} and articleStatus = 0 order by articleCreated desc limit #{offset} , #{pageSize}")


//    // 获取博客首页非置顶文章
//    @Select("select t.oId,t.articleAbstract,t.articleAbstractText,t.articleCommentCount,t.articleCreated,t.articleImg1URL,t.articlePermalink," +
//        "t.articlePutTop,t.articleTitle,t.articleViewCount" +
//        ",GROUP_CONCAT(b3_solo_category.categoryURI) AS categoryURI,GROUP_CONCAT(b3_solo_category.categoryTitle) AS categoryTitle from b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId = \n" +
//        "                        t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
//        "                       b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId " +
//        "where articlePutTop = #{top} and articleStatus = 0 GROUP BY t.oId order by articleCreated desc limit #{offset} , #{pageSize}")
    public List<Map<String, Object>> getArticleByIdV2(Integer offset, Integer pageSize, Integer top);

    // admin端
//    @Select("select distinct t.oId, t.*,b3_solo_category.categoryTitle from b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId =  \n" +
//            "            t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
//            "            b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId   where articlePutTop = #{top} order by articleCreated desc limit #{offset} , #{pageSize}")
//    public List<Map<String, Object>> getAdminArticle(Integer offset, Integer pageSize, Integer top);


    @Select("select  t.oId,t.articleAbstract,t.articleAbstractText,t.articleCommentCount,t.articleCreated,t.articleImg1URL,t.articlePermalink," +
        "t.articlePutTop,t.articleTitle,t.articleViewCount,t.articleTags,t.articleStatus,t.articleViewPwd,t.articleSignId" +
        ", GROUP_CONCAT(b3_solo_category.categoryTitle) AS categoryTitle from b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId = \n" +
        "                        t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
        "                       b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId " +
        "where articlePutTop = #{top} GROUP BY t.oId order by articleCreated desc limit #{offset} , #{pageSize}")
    public List<Map<String, Object>> getAdminArticle(Integer offset, Integer pageSize, Integer top);

    // web端
//    @Select("select t.oId,t.articleAbstract,t.articleAbstractText,t.articleCommentCount,t.articleCreated,t.articleImg1URL,t.articlePermalink," +
//        "t.articlePutTop,t.articleTitle,t.articleViewCount,GROUP_CONCAT(b3_solo_category.categoryURI) as categoryURI,GROUP_CONCAT(b3_solo_category.categoryTitle) as categoryTitle  from  b3_solo_article  as t  LEFT JOIN b3_solo_tag_article on b3_solo_tag_article.article_oId =  \n" +
//        "            t.oId LEFT JOIN b3_solo_category_tag on b3_solo_category_tag.tag_oId = b3_solo_tag_article.tag_oId LEFT JOIN \n" +
//        "            b3_solo_category on b3_solo_category.oId = b3_solo_category_tag.category_oId \n" +
//        "            where articlePutTop = 1 and articleStatus = 0 GROUP BY t.oId order by articleCreated desc")
    public List<Map<String, Object>> getArticleTop(String type);

    @Select("select * from b3_solo_option")
    public List<Map<String, Object>> getArticlePreference();


    @Select("SELECT COUNT(*) FROM b3_solo_article WHERE articleStatus = 0 AND articlePutTop = 0")
    public Integer getArticleTotalV2();

    // 管理端获取总页数
    @Select("select count(*) from b3_solo_article ")
    public Integer getArticleTotalV3(); // 注解模式

    public Integer getArticleTotal(); // 配置模式

    public Integer getArticleViewTotal();

    public List<Map> getArchive();

    //增
    @Options(useGeneratedKeys = true, keyProperty = "zid", keyColumn = "zid")
    @Insert("insert into zip_blog_article" +
        "(articleTitle,articleAbstract,articleTags,articleContent," +
        "articleCreated,articleStatus,articlePermalink," +
        "articleImgURL) " +
        "values" +
        "(#{articleTitle},#{articleAbstract},#{articleTags},#{articleContent}," +
        "#{articleCreated},#{articleStatus},#{articlePermalink}," +
        "#{articleImgURL})")
    public Integer addArticle(Article article);

    // admin端
    @Insert("insert into b3_solo_article (oId,articleTitle,articleAbstract,articleAbstractText,articleTags,articleAuthorId," +
        "articleContent,articlePermalink,articlePutTop,articleCreated,articleUpdated,articleRandomDouble,articleSignId," +
        "articleViewPwd,articleImg1URL,articleStatus,articleCommentCount,articleViewCount) " +
        "values" +
        "(#{id},#{title},#{abstract},#{abstractText},#{tags},#{authorId}," +
        "#{content},#{permalink},#{putTop},#{created},#{updated},#{randomDouble},#{signId},#{pwd},#{imageUrl},#{status},0,0)")
    public Integer addArticleToSolo(Map<String, Object> map);

    // admin
    @Insert("insert into b3_solo_tag_article (oId,article_oId,tag_oId) values (#{id},#{articleId},#{tagId})")
    public Integer addArticleTag(Map<String, Object> map);

    // admin 通过标签名查找id
    @Select("select oId from b3_solo_tag where tagTitle = #{name}")
    public long getTagId(Object name);

    public void addArchive(Map map); //向归档表中插入日期

    //改

    public void updateArticle(Map article);
    // title abstract body imageUrl updated sign pwd permalink


    //    @Update("update b3_solo_article set (articleTitle,articleAbstract,articleAbstractText,articleTags," +
//            "articleContent,articlePermalink,articleUpdated,articleSignId,articleViewPwd,articleImg1URL,articleStatus) " +
//            "values" +
//            "(#{title},#{abstract},${abstract},#{tags},#{content},#{permalink},#{updated},#{signId}," +
//            "#{pwd},#{imageUrl},#{status}) where articlePermalink = #{permalink}")
    @Update("update b3_solo_article set articleTitle=#{title},articleAbstract=#{abstract},articleAbstractText=#{abstract}," +
        "articleTags=#{tags},articleContent=#{content},articlePermalink=#{permalink},articleUpdated=#{updated},articleSignId=#{signId}," +
        "articleViewPwd=#{pwd},articleImg1URL=#{imageUrl},articleStatus=#{status}  where oId = #{id}")
    public void updateSoloArticle(Map article);

    //删
    public void delArticle(Integer zid);

    @Delete("delete from b3_solo_tag_article where article_oId = #{id}")
    public void deleteArticleTag(String id);

    @CacheEvict(value = "article",key = "#zid")
    @Delete("delete from b3_solo_article where oId = #{zid}")
    public void delSoloArticle(String zid);

    //    @Options(useGeneratedKeys = true, keyProperty = "zid")
    @Insert("insert into b3_solo_tag (oId,tagTitle) values(#{zid},#{tagName})")
    public Integer addTags(Tags tags);

    @Select("select * from b3_solo_tag where tagTitle = #{tagName}")
    public Map<String, String> isRepeat(Tags tags);

    @Delete("delete from b3_solo_tag where tagTitle = #{tagName}")
    public void deleteTag(Tags tags);

    @Delete("delete from b3_solo_tag_article where tag_oId = #{tagId}")
    public void deleteTagArticle(String tagId);

    @Delete("delete from b3_solo_category_tag where tag_oId = #{tagId}")
    public void deleteTagCategory(String tagId);

    @Select("select tagTitle from b3_solo_tag")
    public List<String> getTags();

    // 使用xml的方式进行查找
    public Map<String, Object> getNeighborArticle(Map article);
}
